<?php
header("content-type:text/html;charset=utf8");

$redis = new Redis();
$redis->connect("127.0.0.1","6379");
$shop_id =isset($_POST['shop_id'])?$_POST['shop_id']:"";
//print_r($shop_id);die;
$pdo =new PDO("mysql:host=127.0.0.1;dbname=1117;charset=utf8","root","root");
$sql = "select stock from shop where shop_id = $shop_id";
$res = $pdo->query($sql)->fetch();
//for ($i = 1;$i<=$res['stock'];$i++) {
//    $redis->lPush('data'.$shop_id,$i);
//}
if ($redis->lPop("data".$shop_id))
{
    $sql = "update shop set stock = stock-1 where shop_id = $shop_id";
    $info = $pdo->exec($sql);
    if($info)
    {
        $data = array('code' =>1,"msg"=>"抢购成功");
    }
}else{
    $data = array('code' =>1,"msg"=>"对不起，抢光了");
}
echo json_encode($data);
?>